﻿using OASystem.BLL.IService;
using OASystem.Common;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.BLL.Service
{
    public class Document_wait01 : BaseRepository<Handle>, IDocument_wait01
    {
        BaseRepository<Userinfo> uf = new BaseRepository<Userinfo>();
        BaseRepository<Handle> tl = new BaseRepository<Handle>();
        //多个
        public static List<Handle> TasklistResults = new List<Handle>();
        /// <summary>
        /// 发布多人任务
        /// </summary>
        /// <param name="task">TaskList对象</param>
        /// <param name="Publisher">发布人</param>
        /// <param name="DepartId">部门ID</param>
        /// <returns></returns>
        public ResponseResult Recode_tail01ByMore(Handle task, string Publisher, int DepartId)
        {
            #region 查询部门下的所有人
            List<Userinfo> userinfos = new List<Userinfo>();
            Userinfo user = new Userinfo();
            //查询部门下的所有人
            userinfos = uf.QueryList(a => a.DepartId == DepartId);
            #endregion
            //查出发布人信息
            user = uf.Query(a => a.UserName == Publisher);
            //取发布人ID
            task.Publisher = user.UserId;
            //设置发布时间
            task.CompletionTime = DateTime.Now;
            //设置完成状态
            task.Status = 0;
            //循环遍历赋Publisher值且两表添加
            #region 循环遍历赋Publisher值添加
            for (int i = 0; i < userinfos.Count; i++)
            {
                if (userinfos[i].UserName != Publisher)
                {
                    //给每个userinfos赋值
                    task.Oneer = userinfos[i].UserId;
                    TasklistResults.Add(task.DeepCloneObject());
                    //创建实例
                    using (var db = new OasystemEntities1())
                    {
                        using (var tran = db.Database.BeginTransaction())
                        {
                            try
                            {
                                //第一种事务
                                //添加第一张表
                                db.Handle.Add(task);
                                //保存
                                db.SaveChanges();
                                //提交到控制器处理
                                tran.Commit();
                            }
                            catch (Exception ex)
                            {
                                //回滚
                                tran.Rollback();
                            }
                        }
                    }
                }
            }
            #endregion
            ResponseResult responseResult = new ResponseResult();
            responseResult.success = false;
            return responseResult;
        }
        /// <summary>
        /// 发布任务
        /// </summary>
        /// <returns></returns>
        public ResponseResult Publish_task(Handle task, string Publisher)
        {
            Userinfo user = new Userinfo();
            ResponseResult result = new ResponseResult();
            //判断发布人执行人是否存在，查询出发布人的信息
            user = uf.Query(a => a.UserName == Publisher);
            #region 赋值
            //如果不为空就给TaskList对象Publisher赋值
            if (user != null) { task.Publisher = user.UserId; }
            else { result.success = false; return result; }
            task.CompletionTime = DateTime.Now;
            task.Status = 0;
            #endregion
            //接受任务发布结果
            result.success = tl.Add(task);
            if (result.success) { result.msg = "发布成功!"; result.data = task; }
            else { result.msg = "发布失败!"; }
            return result;
        }
    }
}
